
*************************************
*multiple imputation use "ice" module
*************************************

version 15.1
cd "${mypath}\CHNS_project\01_data\02_posted\"

log using "${mypath}\CHNS_project\03_log_files\04_imputation.log", replace

use CHNS_1993_2015.dta,clear

*set up upper and lower bound for work-related variables
foreach i in workmonths1 workmonths2 workdays1 workdays2 workhours1 workhours2 hours_lastw1 hours_lastw2 log_wage_1 log_wage_2 log_bonuse_1 log_bonuse_2 log_income_i log_fam_income log_pc_income year_education degree_education HAI {
	sum `i'
	gen `i'_ll = `i'
	replace `i'_ll = r(min) if `i' == .
	gen `i'_ul = `i'
	replace `i'_ul = r(max) if `i' == .
}

*sort out analytical sample
*drop sample from Beijing, Shanghai, and Chongqing, which were added to the survey later
*keep sample aged from 18 to 55
*generate the number of surveys for each individual
#delimit;	 		 
drop if inlist(T1,11,31,55);
drop if age < 18 | age > 55;
sort IDind wave;
gen serial = 1;
bys IDind:egen times = total(serial);
drop serial;
keep newid homeid IDind wave year1993 year1997 year2000 year2004 year2006 year2009 year2011 year2015
    T1 Liaoning Heilongjiang Jiangsu Shandong Henan Hubei Hunan Guangxi Guizhou location 
	ethnicity gender age age18_25-age51_55 be_migrant migrant_group be_rural_resident be_urban_resident 
	marital_status be_employed employed_last1 workmonths1 workdays1 workhours1 hours_lastw1 
	employed_last2 workmonths2 workdays2 workhours2 hours_lastw2 log_wage_1 log_bonuse_1
	log_wage_2 log_bonuse_2 log_income_i log_fam_income log_pc_income 	
	occupation position work_unit health_insurance year_education degree_education HAI household_size 
	h_group1-h_group5 num_child num_elderly pcgdp child_elder percent_farmer population service_sector 
	workmonths1_ll-HAI_ul times;
sa CHNS_1993_2015_unimputed.dta,replace;

 		 
*multiple imputation
use CHNS_1993_2015_unimputed.dta,clear

*set the work-related variable of inactive sample temporarily according to the module
foreach i in log_wage_1 log_bonuse_1 workmonths1 workdays1 workhours1 hours_lastw1 {
replace `i' = 0  if employed_last1 == 0
}
foreach i in occupation position work_unit {
replace `i' = 0 if be_employed == 0
}

#delimit;	
ice newid homeid IDind wave year1993 year1997 year2000 year2004 year2006 year2009 year2011 year2015
    T1 Liaoning Heilongjiang Jiangsu Shandong Henan Hubei Hunan Guangxi Guizhou location 
	ethnicity gender age age18_25-age51_55 be_migrant migrant_group be_rural_resident be_urban_resident 
	marital_status be_employed employed_last1 workmonths1 workdays1 workhours1 hours_lastw1 
	log_wage_1 log_bonuse_1 log_income_i log_fam_income log_pc_income 
	occupation position work_unit health_insurance year_education degree_education HAI household_size 
	h_group1-h_group5 num_child num_elderly pcgdp child_elder percent_farmer population service_sector 
	workmonths1_ll-HAI_ul times,
saving(CHNS_1993_2015_20_imputed) 
replace seed(12121) m(20) boot 
interval(workhours1: workhours1_ll workhours1_ul,
         hours_lastw1: hours_lastw1_ll hours_lastw1_ul,
         log_wage_1: log_wage_1_ll log_wage_1_ul,
         log_bonuse_1: log_bonuse_1_ll log_bonuse_1_ul,
         log_income_i: log_income_i_ll log_income_i_ul,
         log_fam_income: log_fam_income_ll log_fam_income_ul,
         log_pc_income: log_pc_income_ll log_pc_income_ul,
         year_education: year_education_ll year_education_ul,
         HAI: HAI_ll HAI_ul)
cmd(migrant_group:mlogit,ethnicity:logit,
    workmonths1:ologit,workdays1:ologit,	
	be_employed:logit,employed_last1:logit,
	health_insurance:logit,marital_status:logit,
	degree_education:ologit,
	occupation:mlogit,position:mlogit,work_unit:mlogit)
cond(log_wage_1:employed_last1 == 1\
	 log_bonuse_1:employed_last1 == 1\
     workmonths1:employed_last1 == 1\
	 workdays1:employed_last1 == 1\
     workhours1:employed_last1 == 1\
	 hours_lastw1:employed_last1 == 1\
	 occupation:be_employed == 1\
	 position:be_employed == 1\
	 work_unit:be_employed == 1)
sub(migrant_group:be_migrant be_rural_resident be_urban_resident)
eq(migrant_group: marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 workhours1 
                  occupation position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income 
				  year_education degree_education HAI gender h_group2 h_group3 h_group4 h_group5 
				  num_child num_elderly pcgdp child_elder percent_farmer population service_sector 
				  age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang 
				  Shandong Henan Hubei Hunan Guangxi Guizhou year1997 year2000 year2004 year2006 
				  year2009 year2011 year2015 health_insurance,
   marital_status:migrant_group ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 workhours1 
                  occupation position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income 
				  year_education degree_education HAI gender h_group2 h_group3 h_group4 h_group5 
				  num_child num_elderly pcgdp child_elder percent_farmer population service_sector 
				  age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang Shandong 
				  Henan Hubei Hunan Guangxi Guizhou year1997 year2000 year2004 year2006 year2009 
				  year2011 year2015 health_insurance,
   ethnicity:migrant_group marital_status log_wage_1 log_bonuse_1 workmonths1 workdays1 workhours1 
             occupation position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income 
			 year_education degree_education HAI gender h_group2 h_group3 h_group4 h_group5 
			 num_child num_elderly pcgdp child_elder percent_farmer population service_sector 
			 age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang Shandong 
			 Henan Hubei Hunan Guangxi Guizhou year1997 year2000 year2004 year2006 year2009 year2011 
			 year2015 health_insurance,
   be_employed:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 
               workhours1 hours_lastw1 log_income_i log_fam_income log_pc_income year_education 
			   degree_education HAI gender h_group2 h_group3 h_group4 h_group5 num_child num_elderly 
			   pcgdp child_elder percent_farmer population service_sector age26_30 age31_35 age36_40 
			   age41_45 age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei Hunan Guangxi 
			   Guizhou year1997 year2000 year2004 year2006 year2009 year2011 year2015 health_insurance,
   employed_last1:migrant_group marital_status ethnicity log_income_i log_fam_income log_pc_income 
                  year_education degree_education HAI gender h_group2 h_group3 h_group4 h_group5 
				  num_child num_elderly pcgdp child_elder percent_farmer population service_sector 
				  age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang 
				  Shandong Henan Hubei Hunan Guangxi Guizhou year1997 year2000 year2004 year2006 
				  year2009 year2011 year2015 health_insurance,
   log_wage_1:migrant_group marital_status ethnicity log_bonuse_1 workmonths1 workdays1 workhours1 
              occupation position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income 
			  year_education degree_education HAI gender h_group2 h_group3 h_group4 h_group5 
			  num_child num_elderly pcgdp child_elder percent_farmer population service_sector 
			  age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang Shandong 
			  Henan Hubei Hunan Guangxi Guizhou year1997 year2000 year2004 year2006 year2009 year2011 
			  year2015 health_insurance,
   log_bonuse_1:migrant_group marital_status ethnicity log_wage_1 workmonths1 workdays1 workhours1 
                occupation position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income 
				year_education degree_education HAI gender h_group2 h_group3 h_group4 h_group5 
				num_child num_elderly pcgdp child_elder percent_farmer population service_sector 
				age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang Shandong 
				Henan Hubei Hunan Guangxi Guizhou year1997 year2000 year2004 year2006 year2009 year2011 
				year2015 health_insurance,
   workmonths1:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workdays1 workhours1 
               occupation position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income 
			   year_education degree_education HAI gender h_group2 h_group3 h_group4 h_group5 
			   num_child num_elderly pcgdp child_elder percent_farmer population service_sector 
			   age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang Shandong 
			   Henan Hubei Hunan Guangxi Guizhou year1997 year2000 year2004 year2006 year2009 year2011 
			   year2015 health_insurance,
   workdays1:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workhours1 
             occupation position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income 
			 year_education degree_education HAI gender h_group2 h_group3 h_group4 h_group5 num_child 
			 num_elderly pcgdp child_elder percent_farmer population service_sector age26_30 age31_35 
			 age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei Hunan 
			 Guangxi Guizhou year1997 year2000 year2004 year2006 year2009 year2011 year2015 health_insurance,
   workhours1:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 
              occupation position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income 
			  year_education degree_education HAI gender h_group2 h_group3 h_group4 h_group5 num_child 
			  num_elderly pcgdp child_elder percent_farmer population service_sector age26_30 age31_35 
			  age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei Hunan 
			  Guangxi Guizhou year1997 year2000 year2004 year2006 year2009 year2011 year2015 health_insurance,
   occupation:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 workhours1 
              position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income year_education 
			  degree_education HAI gender h_group2 h_group3 h_group4 h_group5 num_child num_elderly pcgdp 
			  child_elder percent_farmer population service_sector age26_30 age31_35 age36_40 age41_45 
			  age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei Hunan Guangxi Guizhou year1997 
			  year2000 year2004 year2006 year2009 year2011 year2015 health_insurance,
   position:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 workhours1 
            occupation work_unit hours_lastw1 log_income_i log_fam_income log_pc_income year_education 
			degree_education HAI gender h_group2 h_group3 h_group4 h_group5 num_child num_elderly pcgdp 
			child_elder percent_farmer population service_sector age26_30 age31_35 age36_40 age41_45 
			age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei Hunan Guangxi Guizhou year1997 
			year2000 year2004 year2006 year2009 year2011 year2015 health_insurance,
   work_unit:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 workhours1 
             occupation position hours_lastw1 log_income_i log_fam_income log_pc_income year_education 
			 degree_education HAI gender h_group2 h_group3 h_group4 h_group5 num_child num_elderly pcgdp 
			 child_elder percent_farmer population service_sector age26_30 age31_35 age36_40 age41_45 
			 age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei Hunan Guangxi Guizhou year1997 
			 year2000 year2004 year2006 year2009 year2011 year2015 health_insurance,
   hours_lastw1:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 
                workhours1 occupation position work_unit log_income_i log_fam_income log_pc_income 
				year_education degree_education HAI gender h_group2 h_group3 h_group4 h_group5 num_child 
				num_elderly pcgdp child_elder percent_farmer population service_sector age26_30 age31_35 
				age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei Hunan Guangxi 
				Guizhou year1997 year2000 year2004 year2006 year2009 year2011 year2015 health_insurance,
   log_income_i:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 
                workhours1 occupation position work_unit hours_lastw1 log_fam_income log_pc_income 
				year_education degree_education HAI gender h_group2 h_group3 h_group4 h_group5 num_child 
				num_elderly pcgdp child_elder percent_farmer population service_sector age26_30 age31_35 
				age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei Hunan 
				Guangxi Guizhou year1997 year2000 year2004 year2006 year2009 year2011 year2015 health_insurance,
   log_fam_income:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 
                  workhours1 occupation position work_unit hours_lastw1 log_income_i log_pc_income year_education 
				  degree_education HAI gender h_group2 h_group3 h_group4 h_group5 num_child num_elderly pcgdp 
				  child_elder percent_farmer population service_sector age26_30 age31_35 age36_40 age41_45 
				  age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei Hunan Guangxi Guizhou year1997 
				  year2000 year2004 year2006 year2009 year2011 year2015 health_insurance,
   log_pc_income:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 workhours1 
                 occupation position work_unit hours_lastw1 log_income_i log_fam_income  year_education degree_education 
				 HAI gender h_group2 h_group3 h_group4 h_group5 num_child num_elderly pcgdp child_elder percent_farmer 
				 population service_sector age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang 
				 Shandong Henan Hubei Hunan Guangxi Guizhou year1997 year2000 year2004 year2006 year2009 year2011 
				 year2015 health_insurance,
   year_education:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 workhours1 
                  occupation position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income 
				  degree_education HAI gender h_group2 h_group3 h_group4 h_group5 num_child num_elderly pcgdp 
				  child_elder percent_farmer population service_sector age26_30 age31_35 age36_40 age41_45 
				  age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei Hunan Guangxi Guizhou 
				  year1997 year2000 year2004 year2006 year2009 year2011 year2015 health_insurance,
   degree_education:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 
                    workhours1 occupation position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income 
					year_education HAI gender h_group2 h_group3 h_group4 h_group5 num_child num_elderly pcgdp 
					child_elder percent_farmer population service_sector age26_30 age31_35 age36_40 age41_45 
					age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei Hunan Guangxi Guizhou 
					year1997 year2000 year2004 year2006 year2009 year2011 year2015 health_insurance,
   HAI:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 workhours1 
       occupation position work_unit hours_lastw1 log_income_i log_fam_income log_pc_income year_education 
	   degree_education gender h_group2 h_group3 h_group4 h_group5 num_child num_elderly pcgdp child_elder 
	   percent_farmer population service_sector age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 Liaoning 
	   Heilongjiang Shandong Henan Hubei Hunan Guangxi Guizhou year1997 year2000 year2004 year2006 year2009 
	   year2011 year2015 health_insurance,
   health_insurance:migrant_group marital_status ethnicity log_wage_1 log_bonuse_1 workmonths1 workdays1 
                    workhours1 occupation position work_unit hours_lastw1 log_income_i log_fam_income 
					log_pc_income year_education degree_education gender h_group2 h_group3 h_group4 h_group5 
					num_child num_elderly pcgdp child_elder percent_farmer population service_sector age26_30 
					age31_35 age36_40 age41_45 age46_50 age51_55 Liaoning Heilongjiang Shandong Henan Hubei 
					Hunan Guangxi Guizhou year1997 year2000 year2004 year2006 year2009 year2011 year2015 HAI);
#delimit cr

log close
